var fecha_desde, fecha_hasta, rowIds = [], opt_grid;
var repo_incidencia_estado, repo_oportunidad_estado_funnel, repo_ranking_clientes, repo_ranking_vendedores, repo_ranking_productos;


//    mostrarHighcharts();

$(function() {


    opt_grid = $("#fac_det-grid");
    opt_grid.bootgrid({
        ajax: true,
        post: function() {
            return {
                action: "mostrarFacDet"
            };
        },
        selection: true,
        multiSelect: true,
        rowSelect: true,
        keepSelection: true,
        url: "procesos/crud_facturaDetalle.php",
        labels: {
            loading: "Cargando...",
            noResults: "Sin Resultados!",
            infos: " <br> <br> Mostrando {{ctx.start}} - {{ctx.end}} de {{ctx.total}} resultados"
        },
        templates: {
            header: '',
        }
    });



    $("#Facdet_id_producto").select2({
        placeholder: "Seleccione Producto",
        initSelection: function(element, callback) {
            if ($(element).val()) {
                var data = {id: element.val(), text: $(element).attr('selected-text')};
                callback(data);
            }
        },
        ajax: {// instead of writing the function to execute the request we use Select2's convenient helper
            url: "procesos/crud_producto.php",
            type: "post",
            dataType: 'json',
            data: function(term, page) {
                return {
                    search_value: term, // search term
                    action: "AjaxjaxlistProductos"
                };
            },
            results: function(data, page) { // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to alter remote JSON data
                return {results: data};
            }
        },
        allowClear: true
//        multiple: true,
    });


   $("#Facdet_id_factura").select2({
        placeholder: "Seleccione una factura",
        initSelection: function(element, callback) {
            if ($(element).val()) {
                var data = {id: element.val(), text: $(element).attr('selected-text')};
                callback(data);
            }
        },
        ajax: {// instead of writing the function to execute the request we use Select2's convenient helper
            url: "procesos/crud_factura.php",
            type: "post",
            dataType: 'json',
            data: function(term, page) {
                return {
                    search_value: term, // search term
                    action: "AjaxjaxlistFacturas"
                };
            },
            results: function(data, page) { // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to alter remote JSON data
                return {results: data};
            }
        },
        allowClear: true
//        multiple: true,
    });

});


/**
 * Inserta en la tabla un usuario
 * @param {type} $form_id
 * @returns {undefined}
 */
function mostrarReporte($parametros, $url, $object_data, $id_informacion_mostrar, $content_id) {
    viewHighcarts($id_informacion_mostrar, $url, $parametros, function(data) {
        if (data.mostrar) {
            $($content_id).fadeIn(200);
            $object_data.highcharts(data);
        }
    });
}
function insertarFacturaDetalle($form_id) {
    $($form_id).attr('action', "procesos/crud_FacturaDetalle.php");
    console.log($form_id);
    ajaxValidarFormulario({
        formId: $form_id,
        action: "insertar_factura_detalle",
        beforeCall: function() {
            BloquearBotonesModal($form_id);
        },
        successCall: function(data) {
            if (data.success) {
                $("#fac_det-form").trigger('reset');//reinicia todo
                opt_grid.bootgrid('reload');//actualiza l boot
                alert("Se agrego");
            }
        },
        errorCall: function(data) {
            DesBloquearBotonesModal($form_id, 'Crear', 'insertarFacturaDetalle');
        }
    });
}

